var express = require('express');
var router = express.Router();
var async = require('async');
const RestResult = require('../RestResult');
const log = require('../utils/logUtil').log();

const TimetableEntity = require("../models/Timetable").TimetableEntity;
const UserEntity = require("../models/User").UserEntity;

/* GET users listing. */
router.get('/', function (req, res, next) {
});

//数据存入数据库
router.post('/saveAndUpdateTimetableForApp', function (req, res, next) {
    let resData = new RestResult();
    let param = req.body;
    // let userId = param._Id;
    let userId = param.userId;
    log.debug(param);
    TimetableEntity.findOne({userId: userId}, function (err, oldData) {
            //查询错误,错误代码 0
            if (err) {
                resData.code = 1;
                resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                res.send(resData);
            }
            //log.debug(param.timetable.Mon.MorningStart);
            let newData = new TimetableEntity;
            let weekday = req.body.day.substr(0, 3);
            if (weekday == 'Thu') weekday = 'Thur';
            newData.userId = userId;
            newData.timetable = {
                Mon: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Tue: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Wed: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Thur: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Fri: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Sat: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
                Sun: {
                    MorningStart: "",
                    MorningEnd: "",
                    AfternoonStart: "",
                    AfternoonEnd: "",
                    EveningStart: "",
                    EveningEnd: "",
                },
            };
            if (oldData) {
                log.debug(oldData);
                oldData.timetable[weekday] = {
                    MorningStart: param.mons,
                    MorningEnd: param.mone,
                    AfternoonStart: param.afts,
                    AfternoonEnd: param.afte,
                    EveningStart: param.eves,
                    EveningEnd: param.evee
                };
                TimetableEntity.update({userId: userId}, {$set: {"timetable": oldData.timetable}}, function (err, data) {
                    if (err) {
                        resData.code = 1;
                        resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                        res.send(resData);
                    }
                    else {
                        resData.code = 0;
                        log.debug(data);
                        resData.returnValue = data;
                        res.send(resData);

                    }
                });
            }
            else {
                TimetableEntity.insertMany(newData, function (err, doc) {
                    log.debug(newData);
                    log.debug(doc);
                    if (err) {
                        resData.code = 1;
                        resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                        res.send(resData);
                    }
                    else {
                        resData.code = 0;
                        resData.returnValue = doc;
                        res.send(resData);
                    }
                });
            }

        }
    );
});

router.post('/saveAndUpdateTimetableForPC', function (req, res, next) {
    let resData = new RestResult;
    let param = req.body;
    // let userId = param._Id;
    let userId = param.userId;
    log.debug(param);
    TimetableEntity.findOne({userId: userId}, function (err, oldData) {
            //查询错误,错误代码 0
            if (err) {
                resData.code = 1;
                resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                res.send(resData);
            }
            log.debug(param.timetable.Mon.MorningStart);
            let newData = new TimetableEntity;
            newData = {
                userId: userId,
                timetable: {
                    Mon: {
                        MorningStart: param.timetable.Mon.MorningStart,
                        MorningEnd: param.timetable.Mon.MorningEnd,
                        AfternoonStart: param.timetable.Mon.AfternoonStart,
                        AfternoonEnd: param.timetable.Mon.AfternoonEnd,
                        EveningStart: param.timetable.Mon.EveningStart,
                        EveningEnd: param.timetable.Mon.EveningEnd
                    },
                    Tue: {
                        MorningStart: param.timetable.Tue.MorningStart,
                        MorningEnd: param.timetable.Tue.MorningEnd,
                        AfternoonStart: param.timetable.Tue.AfternoonStart,
                        AfternoonEnd: param.timetable.Tue.AfternoonEnd,
                        EveningStart: param.timetable.Tue.EveningStart,
                        EveningEnd: param.timetable.Tue.EveningEnd
                    },
                    Wed: {
                        MorningStart: param.timetable.Wed.MorningStart,
                        MorningEnd: param.timetable.Wed.MorningEnd,
                        AfternoonStart: param.timetable.Wed.AfternoonStart,
                        AfternoonEnd: param.timetable.Wed.AfternoonEnd,
                        EveningStart: param.timetable.Wed.EveningStart,
                        EveningEnd: param.timetable.Wed.EveningEnd
                    },
                    Thur: {
                        MorningStart: param.timetable.Thur.MorningStart,
                        MorningEnd: param.timetable.Thur.MorningEnd,
                        AfternoonStart: param.timetable.Thur.AfternoonStart,
                        AfternoonEnd: param.timetable.Thur.AfternoonEnd,
                        EveningStart: param.timetable.Thur.EveningStart,
                        EveningEnd: param.timetable.Thur.EveningEnd
                    },
                    Fri: {
                        MorningStart: param.timetable.Fri.MorningStart,
                        MorningEnd: param.timetable.Fri.MorningEnd,
                        AfternoonStart: param.timetable.Fri.AfternoonStart,
                        AfternoonEnd: param.timetable.Fri.AfternoonEnd,
                        EveningStart: param.timetable.Fri.EveningStart,
                        EveningEnd: param.timetable.Fri.EveningEnd
                    },
                    Sat: {
                        MorningStart: param.timetable.Sat.MorningStart,
                        MorningEnd: param.timetable.Sat.MorningEnd,
                        AfternoonStart: param.timetable.Sat.AfternoonStart,
                        AfternoonEnd: param.timetable.Sat.AfternoonEnd,
                        EveningStart: param.timetable.Sat.EveningStart,
                        EveningEnd: param.timetable.Sat.EveningEnd
                    },
                    Sun: {
                        MorningStart: param.timetable.Sun.MorningStart,
                        MorningEnd: param.timetable.Sun.MorningEnd,
                        AfternoonStart: param.timetable.Sun.AfternoonStart,
                        AfternoonEnd: param.timetable.Sun.AfternoonEnd,
                        EveningStart: param.timetable.Sun.EveningStart,
                        EveningEnd: param.timetable.Sun.EveningEnd
                    }
                }
            };
            if (oldData) {
                log.debug(oldData);
                TimetableEntity.update({userId: userId}, {$set: {"timetable": newData.timetable}}, function (err, data) {
                    if (err) {
                        resData.code = 1;
                        resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                        res.send(resData);
                    }
                    else {
                        resData.code = 0;
                        log.debug(data);
                        resData.returnValue = data;
                        res.send(resData);

                    }
                });
            }
            else {
                TimetableEntity.insertMany(newData, function (err, doc) {
                    log.debug(newData);
                    log.debug(doc);
                    if (err) {
                        resData.code = 1;
                        resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                        res.send(resData);
                    }
                    else {
                        resData.code = 0;
                        resData.returnValue = doc;
                        res.send(resData);
                    }
                });
            }

        }
    );
});

//从数据库取出数据
router.post('/getTimetable', function (req, res, next) {
    let userId = req.body.userId;
    let resData = new RestResult;
    log.debug(userId);
    if (!userId) {
        resData.code = RestResult.ILLEGAL_ARGUMENT_ERROR_CODE;
        resData.errorReason = '未登录!';
        res.send(resData);
        return;
    }
    TimetableEntity.findOne({userId: userId}, function (err, data) {
        log.debug(data);
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else {
            resData.code = 0;
            resData.returnValue = data;
            res.send(resData);
        }
    });

});

router.post('/getTimeIDForApp', function (req, res, next) {
    let param = req.body;
    let day = param.day;
    let timeStart = param.time + "Start";
    let timeEnd = param.time + "End";
    let resData = new RestResult;
    let flag = false;
    let user = [];
    TimetableEntity.find({}, function (err, docs) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        if (docs) {
            async.each(docs, function (doc, callback) {
                if (doc.timetable[day][timeStart] != null && doc.timetable[day][timeStart] != "" && doc.timetable[day][timeEnd] != null && doc.timetable[day][timeEnd] != "") {
                    let userId = doc.userId;
                    let name = "";
                    // log.debug(userId);
                    UserEntity.findOne({_id: userId}, function (err, data) {
                        // log.debug(data);
                        if (err) {
                            resData.code = 1;
                            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                            res.send(resData);
                        }
                        if (data) {

                            name = data.name;
                            user.push({name: name, time: doc.timetable[day]});
                            flag = true;
                            log.debug(user);
                            callback();
                        }
                        else {
                            callback();
                        }
                    });
                }
                else {
                    callback();
                }
            }, function (err) {
                if (err) {
                    console.log("err is:err");
                    return;
                }
                if (flag) {
                    resData.code = 0;
                    resData.returnValue = user;
                    //log.debug(user);
                    res.send(resData);
                }
                else {
                    resData.code = 2;
                    resData.errorReason = "未找到数据";
                    //log.debug(user);
                    res.send(resData);
                }
            });

        }
        else {
            resData.code = 2;
            resData.errorReason = "未找到数据";
            res.send(resData);
        }
    });
});
/*router.post('/getTimeIDForApp',function (req, res, next){
 !
 });*/
module.exports = router;
